package com.yue.ssa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yue.ssa.bean.RespBean;
import com.yue.ssa.bean.Role;
import com.yue.ssa.common.Constant;
import com.yue.ssa.exception.MyException;
import com.yue.ssa.mapper.RoleMapper;
import com.yue.ssa.service.RoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author zhengyl
 * @description
 * @date 2020-04-08 9:34
 */
@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Override
    public List<Role> getRolesByUid(String uid) {
        return this.baseMapper.getRolesByUid(uid);
    }

    @Override
    public RespBean get(String id) {
        Role role = this.baseMapper.getById(id);
        if (role == null) throw new MyException(Constant.BEANID_INVALID);
        return RespBean.ok(role);
    }

    @Override
    public RespBean add(Role role) {
        if (StringUtils.isBlank(role.getName()) || StringUtils.isBlank(role.getNameZh())) {
            throw new MyException(Constant.INCOMPLETE_INFORMATION);
        }
        if (nameIsExist(role.getName())) {
            throw new MyException(Constant.ROLENAME_REPEAT);
        }
        role.setCreateTime(new Date());
        return RespBean.ok(this.save(role));
    }

    @Override
    public RespBean update(Role role) {
        if (StringUtils.isBlank(role.getId()) || StringUtils.isBlank(role.getName()) || StringUtils.isBlank(role.getNameZh())) {
            throw new MyException(Constant.INCOMPLETE_INFORMATION);
        }
        Role r = this.getById(role.getId());
        if (r == null) throw new MyException(Constant.BEANID_INVALID);
        if (!role.getName().equals(r.getName()) && nameIsExist(role.getName())) {
            throw new MyException(Constant.ROLENAME_REPEAT);
        }
        return RespBean.ok(this.updateById(role));
    }

    @Override
    public RespBean page(String keyword, Integer pageNo, Integer pageSize) {
        IPage<Role> page = new Page<>();
        page.setCurrent(pageNo);
        page.setSize(pageSize);
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(keyword)) {
            queryWrapper.lambda().like(Role::getName, keyword);
            queryWrapper.lambda().or().like(Role::getNameZh, keyword);
        }
        queryWrapper.lambda().orderByDesc(Role::getCreateTime);
        return RespBean.ok(this.page(page, queryWrapper));
    }

    @Override
    public boolean nameIsExist(String name) {
        Map<String, Object> params = new HashMap<>();
        params.put("name", name);
        return this.listByMap(params).size() > 0;
    }

    @Override
    public RespBean delete(String[] ids) {
        if (ids == null || ids.length == 0) return RespBean.ok(false);
        return RespBean.ok(this.removeByIds(Arrays.asList(ids)));
    }
}
